{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8be07deb",
   "metadata": {},
   "source": [
    ">*\n",
    "购物车程序项目：\n",
    "\n",
    "要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "　　   2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "　　   3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "　　   4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bb2844a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# input输入\n",
    "# 格式化打印：占位符、.format 的输出\n",
    "# *列表：存储类型相同的一组数据，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4dfd0fbc",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额：88\n",
      "------欢迎选购，请输入编号购买，或输入T退出------\n",
      "商品编号：0,商品名称：巧克力，商品价格：9\n",
      "商品编号：1,商品名称：烤鸡，商品价格：35\n",
      "商品编号：2,商品名称：牛肉卷，商品价格：28\n",
      "商品编号：3,商品名称：麦旋风，商品价格：12\n",
      "请正确输入购买商品的编号：2\n",
      "您的牛肉卷已加入到购物车，您的账户余额还有：60元\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-184de21cf95c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     39\u001b[0m                 \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'您的余额不足，请更换支付方式。'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     40\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mgoods_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mchoice\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;32min\u001b[0m \u001b[0muser_shopping_list\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 41\u001b[1;33m                 \u001b[0muser_shopping_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcount\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     42\u001b[0m                 \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0muser_shoppong_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     43\u001b[0m     \u001b[1;32melif\u001b[0m \u001b[0mchoice\u001b[0m \u001b[1;33m==\u001b[0m\u001b[1;34m'T'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int'"
     ]
    }
   ],
   "source": [
    "# 1.商品数据信息，使用列表的嵌套（含商品名称、价格）、列表有特定的索引值\n",
    "goods_list = [  \n",
    "    ['巧克力',9],\n",
    "    [ '烤鸡',35],\n",
    "    ['牛肉卷',28],\n",
    "    ['麦旋风',12]\n",
    "    ]\n",
    "\n",
    "user_shopping_list = []\n",
    "# 2.用户输入使用input\n",
    "Alipay = int(input('请输入您的余额：'))\n",
    "\n",
    "while True:\n",
    "# 循环遍历商品列表，将商品信息按照一定格式输出\n",
    "# 一定格式： 商品编号：xx,商品名称：xx，商品价格：xx\n",
    "    print('------欢迎选购，请输入编号购买，或输入T退出------')\n",
    "    for i in range(len(goods_list)): #range 会根据商品列表的长度变化而变化\n",
    "        print('商品编号：%s,商品名称：%s，商品价格：%s'%(i,goods_list[i][0],goods_list[i][1]))\n",
    "        # i表示商品编号\n",
    "    # 2.让用户输入商品编号进行商品的购买\n",
    "        # 2.1 用户输入的内容是否符合规范（用户买的东西有没有）？\n",
    "\n",
    "        # 思考：用户除了输入数值外，有没有可能输入不是数值\n",
    "\n",
    "    choice = input('请正确输入购买商品的编号：')\n",
    "    if choice.isdigit():# isdigit判断输入字符串内容是否是数值\n",
    "        choice = int(choice)\n",
    "        if choice >= len(goods_list):\n",
    "            # 2.3 如果没有该商品，返回没有\n",
    "            print('您选择的商品不存在')\n",
    "        else:\n",
    "             # 2.2 如果有 判断金额是否够： 用户金额Alipay 商品的金额 goods_list[choice][1]\n",
    "            if Alipay > goods_list[choice][1]:\n",
    "                # 用户余额够 1.增加该商品搭配用户购物车 2.扣除余额\n",
    "                user_shopping_list.append(goods_list[choice])\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                print('您的%s已加入到购物车，您的账户余额还有：%s元'%(goods_list[choice][0],Alipay))\n",
    "            else:\n",
    "                print('您的余额不足，请更换支付方式。')\n",
    "            if goods_list[choice] in user_shopping_list:\n",
    "                user_shopping_list.count += 1\n",
    "                print(user_shoppong_list)\n",
    "    elif choice =='T':\n",
    "        # 用户退出购买\n",
    "        print('用户购物车：',user_shopping_list)\n",
    "        print('您的余额还有：',Alipay)\n",
    "        break\n",
    "    else:\n",
    "        print('请输入商品编号！！！')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "233e3051",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可优化的地方：显示购买的数量是2\n",
    "# 购物车内容的打印可以进行优化，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3ea48c6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "705e5a09",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4b67a55",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a398277",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "404c3dd4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82f9f5cf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1346758e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "372dd591",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
